﻿using Recast;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using static Recast.RecastGlobal;

namespace RecastNetTest
{
	internal unsafe class Program
	{
		static void Main(string[] args) 
		{
			Test();

			while (true)
			{
				GC.Collect();
				GC.Collect(1);
				GC.Collect(2);
				GC.Collect(3);
				GC.WaitForPendingFinalizers();
				
				var test = Ptrs;

				Console.ReadLine();
			}

			
		}

		static void Test()
		{
			var sample = CreateSoloMesh();
			var buildSettings = BuildSettings.Default;
			//buildSettings.agentRadius = 0.2f;
			SetBuildSettings(sample, buildSettings);
			var r = LoadMeshFile(sample, @"D:\\recastnavigation_share\\RecastDemo\\Bin\\Meshes\\dungeon.obj");
			//var r2 = LoadNavMesh(sample, "D:/recast-pathfinding/Assets/demo2NavMesh.bin");
			//var text = File.ReadAllText(@"D:\\recastnavigation_share\\RecastDemo\\Bin\\Meshes\\dungeon.obj");
			//var r3 = RecastDll.LoadObjText(sample, text, text.Length);
			
			var r1 = Build(sample);

			float* array = (float*)Marshal.AllocHGlobal(sizeof(float) * 65535);
			int count;
			GetDrawNavMesh(sample, array, out count);

			float* m_spos = stackalloc float[] { -41.1599998f, 6.48999977f, -49.7900009f };
			float* m_epos = stackalloc float[] { 48.2099991f, 12.3400002f, 39.3199997f };

			var m_straightPath = (float*)Marshal.AllocHGlobal(sizeof(float) * 4096 * 3);

			Stopwatch stopwatch = Stopwatch.StartNew();
			for (int i = 0; i < 100000; i++)
			{
				FindPathStraight(sample, m_spos, m_epos, m_straightPath, out int outPointCount, dtStraightPathOptions.DT_STRAIGHTPATH_ALL_CROSSINGS);

				//List<Vector3> paths = new List<Vector3>();
				//for (int i = 1; i < outPointCount; i++) //为什么不能要最后一条线? 因为后面一条线偶尔出现y=1的问题, 最后一个不要也不影响
				//{
				//	int v = i * 3;
				//	paths.Add(new Vector3(m_straightPath[v - 3], m_straightPath[v - 2] + 0, m_straightPath[v - 1])); //a线
				//	paths.Add(new Vector3(m_straightPath[v + 0], m_straightPath[v + 1] + 0, m_straightPath[v + 2])); //b线
				//}
			}
			stopwatch.Stop();
			Console.WriteLine("C# 10万次寻路:" + stopwatch.Elapsed);

			//Console.ReadLine();
		}
	}

	public struct Vector3 
	{
		public float x;
		public float y;
		public float z;

		public Vector3(float x, float y, float z)
		{
			this.x = x;
			this.y = y;
			this.z = z;
		}

		public override string ToString()
		{
			return $"({x.ToString("f1")},{y.ToString("f1")},{z.ToString("f1")})";
		}
	}
}
